package com.wlyuan.open.domain.source.dispatcher.conversion;

import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.domain.source.dispatcher.customer.CreateCustomer;
import com.wlyuan.open.domain.source.repository.cache.OpenOrderCacheRepository;
import com.wlyuan.open.dto.top.OpenTopExtensionOrderSyncDTO;
import com.wlyuan.open.remote.facade.CustomerFacade;
import com.wlyuan.open.remote.facade.GoodsFacade;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @author lijing
 * @description
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OpenDispatcherTopExtensionConversion implements OpenDispatcherConversion<OpenTopExtensionOrderSyncDTO> {
    private final BaseOpenDispatcherConversion baseOpenDispatcherConversion;
    private final OpenOrderCacheRepository openOrderCacheRepository;
    private final GoodsFacade goodsFacade;
    private final CustomerFacade customerFacade;

    @Override
    public void convert(OpenTopExtensionOrderSyncDTO source) {
        var tenant = source.getTenantId();
        if (LongUtils.notNullLong(tenant)) {
            Long tenantId = baseOpenDispatcherConversion.tenant(tenant);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenTopExtensionOrderSyncDTO租户不存在:" + tenant), source);
            source.setTenantId(tenantId);
        }
        var cTenant = source.getCustomerTenantId();
        if (LongUtils.notNullLong(cTenant)) {
            Long tenantId = baseOpenDispatcherConversion.tenant(cTenant);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenTopExtensionOrderSyncDTO客户租户不存在:" + cTenant), source);
            source.setCustomerTenantId(tenantId);
        }

        var order = source.getOrderId();
        if (LongUtils.notNullLong(order)) {
            var orderId = openOrderCacheRepository.convert(order);
            AssertUtils.assertNotNull(orderId, new ValueError("OpenTopExtensionOrderSyncDTO订单不存在:" + order), source);
            source.setOrderId(orderId);
        }
        //制单人
        if (LongUtils.notNullLong(source.getCreateMan())) {
            var createId = baseOpenDispatcherConversion.user(source.getCreateMan());
            AssertUtils.assertNotNull(createId, new ValueError("OpenOrderSyncDTO制单人不存在:" + source.getCreateMan()), source);
            source.setCreateMan(createId);
        }
        //客户
        if (LongUtils.notNullLong(source.getCustomerId())) {
            var customer = baseOpenDispatcherConversion.customer(source.getCustomerId(), source.getCustomerType(), source.getCustomerTenantId());
            AssertUtils.assertNotNull(customer, new ValueError("OpenOrderSyncDTO客户不存在:" + source.getCustomerId()), source);
            source.setCustomerId(customer.getCustomerId());
            if (StringUtils.isNotBlank(customer.getCustomerName())) {
                source.setCustomerName(customer.getCustomerName());
            }
        } else {
            //为空或者0 , 创建线下客户
            var customer = customerFacade.createCustomer(new CreateCustomer(source.getCName(), source.getContactMobile(), source.getContactName(), source.getCustomerTenantId(), source.getCreateMan()));
            source.setCustomerId(customer.getCustomerId());
        }

        source.setCreateBy(0L);
        source.setUpdateBy(0L);

        //物品集合
        var goodsList = source.getGoodsList();
        if (!CollectionUtils.isEmpty(goodsList)) {
            goodsList.forEach(goods -> {
                var goodsDetailDTO = goodsFacade.createGoods(goods, source.getCustomerId(), source.getTenantId(), source.getCreateMan());
                goods.setGoodsId(goodsDetailDTO.getGoodsId());

                goods.setCreateBy(0L);
                goods.setUpdateBy(0L);
            });
        }
    }
}
